package com.example.zyfyptlzx729.model;

import com.example.zyfyptlzx729.bean.VideoBean;
import com.example.zyfyptlzx729.common.Common;
import com.example.zyfyptlzx729.iface.VideoListener;
import com.example.zyfyptlzx729.iface.Videoiface;
import com.example.zyfyptlzx729.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VideoModel implements Videoiface {
    private Retrofit retrofit;
    public VideoModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String sessionID, String mod, int page, final VideoListener videoListener) {
        VideoService service=retrofit.create(VideoService.class);
        Call<List<VideoBean>> call=service.getVideoList(sessionID,mod,page);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    videoListener.onResponse(response.body());
                }
                else{
                    videoListener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                videoListener.onFail(t.toString());
            }
        });
    }
}
